# Hash Password
hash = (password) ->
    secure = '~!@#$%^&*()_+[]'
    md5 sha1(password) + secure

loginController = ($scope) ->
    $scope.applogin = ->
        $scope.app_login_hash = hash $scope.app_login_password
        return
    return

registerController = ($scope) ->
    # Send request to Server to check Existing Email
    isExistedEmail = ->
        state = undefined

        send 'checkEmail', email: $scope.app_input_email, (callback) ->
            state = callback
            return

        if state['message'] is 'true'
            true
        else
            false

    # Remove all popover
    resetTips = ->
        removeTips '#app_register_fullname input'
        removeTips '#app_register_email input'
        removeTips '#app_register_password input'
        removeTips '#app_register_confirm_password input'
        return

    # Check Full name when user pressed
    $('#app_register_fullname input').keyup (e) ->
        resetTips()
        if /[0-9~`!@#$%^&*\(\)_\+\{\}\|\[\]\\\-=:";',.\/<>\?]+/.test $scope.app_input_fullname
            showTips '#app_register_fullname input', 'Full name does not have any special characters.'
            this.focus()
        return

    # Check Email when user pressed
    $('#app_register_email input').keyup (e) ->
        resetTips()
        email = $scope.app_input_email
        if  /([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/.test(email) is false and email.length > 0
            showTips '#app_register_email input', 'Email is invalid.'
            this.focus()
        return

    # Check Full Name when focus out
    $('#app_register_fullname input').focusout ->
        resetTips()
        full_name = $scope.app_input_fullname
        isChanged = false

        if full_name is undefined
            return
        if full_name.length is 1
            showTips '#app_register_fullname input', 'Full name must be at least 2 characters.'
            isChanged = true

        if full_name.length isnt 0 and isChanged
            this.focus()

        return

    # Check Email when focus out
    $('#app_register_email input').focusout ->
        resetTips()
        email = $scope.app_input_email
        isChanged = false

        if email is undefined or email.length is 0
            return
        if isExistedEmail()
            showTips '#app_register_email input', 'This email is not available.'
            isChanged = true

        if email.length isnt 0 and isChanged
            this.focus()

        return

    # Check Password when focus out
    $('#app_register_password input').focusout ->
        resetTips()
        password = $scope.app_input_password
        isChanged = false

        if password is undefined
            return
        if 0 < password.length < 6
            showTips '#app_register_password input', 'Password must be at least 6 characters.'
            isChanged = true

        if password.length isnt 0 and isChanged
            this.focus()

        return

    # Check Confirm Password when focus out
    $('#app_register_confirm_password input').focusout ->
        resetTips()
        confirm_password = $scope.app_input_confirm_password

        if  confirm_password isnt $scope.app_input_password
            showTips '#app_register_confirm_password input', 'Confirm password is not match.'

        return


    # Click SignUp button
    $scope.signUp = ->

        # Variables
        full_name = $scope.app_input_fullname
        email = $scope.app_input_email
        password = $scope.app_input_password
        confirm_password = $scope.app_input_confirm_password

        # Reset Popover
        resetTips()

        # Check Full Name
        if full_name is undefined or full_name.length is 0
            showTips '#app_register_fullname input', 'Full name must not be left blank.'
            $('#app_register_fullname input').focus()
            return

        # Check Email
        if email is undefined or email.length is 0
            showTips '#app_register_email input', 'Email must not be left blank.'
            $('#app_register_email input').focus()
            return

        # Check Password
        if password is undefined or password.length is 0
            showTips '#app_register_password input', 'Password must not be left blank.'
            $('#app_register__password input').focus()
            return

        # Check Confirm Password
        if  confirm_password isnt password
            showTips '#app_register_confirm_password input', 'Confirm password is not match.'
            $('#app_register_confirm_password input').focus()
            return

        # Create Hash
        $scope.app_register_hash = hash password

        # Convert to Submit and send Request to Server
        $('#app_submit').prop('type', 'submit')

        return
    return

confirmController = ($scope) ->
    # Variables
    steps = 3
    $scope.countries = app.countries;
    $scope.languages = app.languages;
    $scope.questions = [
        {question: 'Who is your daddy?'},
        {question: 'Are you crazy?'},
        {question: 'How many virus in your paint?'}
    ]

    changeStep = (selector) ->
        res = selector.split '-'
        pos = parseInt res[2]

        # Set Active
        $('#step' + pos).removeClass('active')

        if res[0] is 'next'
            pos += 1
        else
            pos -= 1

        $('#step' + pos).addClass('active')

        pos -= 1

        # Reset classes for all steps
        $steps = $('#StepsRegistration').find('.steps li')
        $steps.removeClass('active').removeClass('complete')
        $steps.find('span.badge').removeClass('badge-info').removeClass('badge-success')

        # Set class for all previous steps
        prevSelector = '.steps li:lt(' + pos + ')'
        $prevSteps = $('#StepsRegistration').find(prevSelector)
        $prevSteps.addClass('complete')
        $prevSteps.find('span.badge').addClass('badge-success')

        # Set class for current step
        currentSelector = '.steps li:eq(' + pos + ')'
        $currentStep = $('#StepsRegistration').find(currentSelector)
        $currentStep.addClass('active')
        $currentStep.find('span.badge').addClass('badge-info')
        return

    # Click Next/Previous button/label
    $('#next-step-1').click ->
        changeStep(this.id)
        return

    $('#next-step-2').click ->
        changeStep(this.id)
        return

    $('#prev-step-2').click ->
        changeStep(this.id)
        return

    $('#prev-step-3').click ->
        changeStep(this.id)
        return

    # Secret question
    setTimeout ->
        $('.secret-question').click ->
            $('#question').prop 'value', this.textContent.trim()
            return

        return
    , 500

    return

# Add Popover
showTips = (selector, content) ->
    options = (content) ->
        animation: 'true'
        deplay: 1000
        placement: 'left'
        content: content
        container: 'body'
    $(selector).popover options content
    $(selector).popover 'show'
    return

# Remove Popover
removeTips = (selector) ->
    $(selector).popover 'destroy'
    return

$('#app_language_box a').click (e) ->
    e.preventDefault()
    $(this).tab('show')


$('.app_register_question_suggest li').click = ->
    text = $(this).find('a')[0].innerHTML
    $('.app_register_question').val(text)
    return
###